package com.mywang.weitravel.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.mywang.weitravel.R;
import com.mywang.weitravel.model.HomeListBean;
import com.mywang.weitravel.modules.manbushanghai.ShangHaiActivity;
import com.mywang.weitravel.modules.redlocate.RedLocateActivity;
import com.mywang.weitravel.util.ImageLoader;

import java.util.List;

/**
 * Created by wangmingyun on 2019/12/6.
 */
public class HomeListAdapter extends RecyclerView.Adapter<HomeListAdapter.HomeListHolder> {

    private List<HomeListBean> mData;

    private Context mContext;

    public HomeListAdapter(Context context, List<HomeListBean> mData) {
        this.mContext = context;
        this.mData = mData;
    }

    @NonNull
    @Override
    public HomeListHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_home, parent, false);
        return new HomeListHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull HomeListHolder holder, int position) {
        HomeListBean homeListBean = mData.get(position);
        holder.tvName.setText(homeListBean.getName());
        ImageLoader.getInstance().displayImage(mContext, homeListBean.getFilepath(), holder.ivPic);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (position == 0) {
                    Intent intent = new Intent(mContext, RedLocateActivity.class);
                    mContext.startActivity(intent);
                } else if (position == 1) {
                    Intent intent = new Intent(mContext, ShangHaiActivity.class);
                    mContext.startActivity(intent);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    class HomeListHolder extends RecyclerView.ViewHolder {
        TextView tvName;
        ImageView ivPic;

        public HomeListHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
            ivPic = itemView.findViewById(R.id.iv_pic);
        }
    }
}
